﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;

namespace Kiiro.Model.HttpHandler
{
    public class KiiroASyncFileUploadHandler : IHttpAsyncHandler
    {
        /*  ADD TO WEB CONFIG:
            
            <httpHandlers>
                <add verb="*" path="*.aSyncFileUpload" type="Kiiro.Model.HttpHandler.KiiroASyncFileUploadHandler, Kiiro.Model, Version=1.2.0.7, Culture=neutral, PublicKeyToken=5b2f4277f61b7816" />
            </httpHandlers>
   
            <system.webServer>
                <handlers>
                    <add verb="*" path="*.aSyncFileUpload" name="KiiroASyncFileUploadHandler" modules="IsapiModule"
                        scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
                        type="Kiiro.Model.HttpHandler.KiiroASyncFileUploadHandler, Kiiro.Model, Version=1.2.0.7, Culture=neutral, PublicKeyToken=5b2f4277f61b7816" />
                </handlers>
            </system.webServer>
        */

        public bool IsReusable { get { return false; } }

        public KiiroASyncFileUploadHandler ( ) { }

        public IAsyncResult BeginProcessRequest ( HttpContext context, AsyncCallback cb, object extraData )
        {
            context.Response.Write ( "Begin process request." );
            ASyncFileUpload aSyncFileUpload = new ASyncFileUpload ( cb, context, extraData );
            aSyncFileUpload.StartASyncWork ( );
            return aSyncFileUpload;
        }

        public void EndProcessRequest ( IAsyncResult result )
        {
        }

        public void ProcessRequest ( HttpContext context )
        {
            throw new NotImplementedException ( );
        }
    }

    internal class ASyncFileUpload : IAsyncResult
    {
        private bool isCompleted;
        private object aSyncState;
        private AsyncCallback aSyncCallback;
        private HttpContext httpContext;

        public object AsyncState { get { return aSyncState; } }
        public WaitHandle AsyncWaitHandle { get { return null; } }
        public bool CompletedSynchronously { get { return false; } }
        public bool IsCompleted { get { return isCompleted; } }

        public ASyncFileUpload ( AsyncCallback callback, HttpContext context, Object state )
        {
            aSyncCallback = callback;
            httpContext = context;
            aSyncState = state;
            isCompleted = false;
        }

        public void StartASyncWork ( )
        {
            ThreadPool.QueueUserWorkItem ( new WaitCallback ( ASyncWork ), null );
        }

        private void ASyncWork ( Object workState )
        {
            httpContext.Response.Write ( "Hello World from ASyncHandler!" );
            isCompleted = true;
            aSyncCallback ( this );
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/HttpHandler/KiiroASyncFileUploadHandler.cs $
 * 
 * 8     4/07/10 2:33p Camerons
 * 
 * 7     2/24/10 5:14p Camerons
 * 
 * 6     2/23/10 11:46a Camerons
 * 
 * 5     2/16/10 10:11a Camerons
 * 
 * 4     2/09/10 12:48p Camerons
 * 
 * 3     2/08/10 5:04p Camerons
 * 
 * 2     1/25/10 1:17p Camerons
 * 
 * 1     1/18/10 9:56a Camerons
 * 
 *************************************************************************/